package sv.com.seguros.dao.asg;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import sv.com.seguros.dao.BaseDaoImpl;
import sv.com.seguros.dto.asg.AsgAbono;
import sv.com.seguros.exception.WebException;

public class AsgAbonoDaoImpl extends BaseDaoImpl implements AsgAbonoDao {

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllByAsgPolizaId(Long asgPolizaId) throws WebException{
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(AsgAbono.class);
			criteria.add(Restrictions.eq("asgPoliza.asgPolizaId", asgPolizaId));
			criteria.addOrder(Order.asc("asgAbonoNumeroCuota"));
			criteria.createAlias("asgPoliza", "asgPoliza");
			criteria.createAlias("asgPoliza.asgCliente", "asgCliente");
			criteria.setProjection(Projections.projectionList().
				add(Projections.property("asgAbonoId")).	
				add(Projections.property("asgAbonoNumeroCuota")).	
				add(Projections.property("asgAbonoMonto")).	
				add(Projections.property("asgAbonoFechaLimite")).	
				add(Projections.property("asgPoliza.asgPolizaId")).	
				add(Projections.property("asgPoliza.asgPolizaNumero")).
				add(Projections.property("asgPago.asgPagoId"))
			);
			return (List<Object[]>) findByCriteria(criteria);
		} catch (Exception e) {
			throw new WebException("Problemas al tratar de obtener el detalle de abonos");
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> findAllPendientes(Long asgClienteId) throws WebException{
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(AsgAbono.class);
			criteria.createAlias("asgPoliza", "asgPoliza");
			criteria.createAlias("asgPoliza.asgCliente", "asgCliente");
			criteria.setProjection(Projections.projectionList().
					add(Projections.property("asgAbonoId")).	
					add(Projections.property("asgAbonoNumeroCuota")).	
					add(Projections.property("asgAbonoMonto")).	
					add(Projections.property("asgAbonoFechaLimite")).	
					add(Projections.property("asgPoliza.asgPolizaNumero")).
					add(Projections.property("asgCliente.asgClienteId"))
			);
			criteria.add(Restrictions.eq("asgCliente.asgClienteId", asgClienteId));
			criteria.add(Restrictions.isNull("asgPago.asgPagoId"));
			criteria.addOrder(Order.asc("asgPoliza.asgPolizaNumero"));
			return (List<Object[]>) findByCriteria(criteria);
		} catch (Exception e) {
			throw new WebException("Problemas al tratar de obtener las cuotas pendientes de pago");
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> findAllPagos(Long asgClienteId) throws WebException{
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(AsgAbono.class);
			criteria.createAlias("asgPoliza", "asgPoliza");
			criteria.createAlias("asgPoliza.ctgAseguradora", "ctgAseguradora");
			criteria.createAlias("asgPago", "asgPago");
			criteria.createAlias("asgPoliza.asgCliente", "asgCliente");
			criteria.setProjection(Projections.projectionList().
					add(Projections.property("asgAbonoNumeroCuota")).	
					add(Projections.property("asgAbonoMonto")).	
					add(Projections.property("asgAbonoFechaLimite")).
					add(Projections.property("asgPoliza.asgPolizaNumero")).
					add(Projections.property("asgPago.asgPagoId")).
					add(Projections.property("asgPago.asgPagoFecha")).
					add(Projections.property("asgCliente.asgClienteId")).
					add(Projections.property("ctgAseguradora.ctgAseguradoraNombre"))
			);
			criteria.add(Restrictions.eq("asgCliente.asgClienteId", asgClienteId));
			criteria.add(Restrictions.isNotNull("asgPago.asgPagoId"));
			criteria.addOrder(Order.asc("asgPoliza.asgPolizaNumero"));
			return (List<Object[]>) findByCriteria(criteria);
		} catch (Exception e) {
			throw new WebException("Problemas al tratar de obtener el historial de cuotas pagadas");
		}
	}
}
